package com.java.zhuiyun.api.front_desk_user.service.impl;

import com.java.zhuiyun.api.alipay.controller.AliPay;
import com.java.zhuiyun.api.front_desk_user.entity.FrontDeskUser;
import com.java.zhuiyun.api.front_desk_user.mapper.FrontDeskUserMapper;
import com.java.zhuiyun.api.front_desk_user.model.FrontDeskUserModels;
import com.java.zhuiyun.api.front_desk_user.service.FrontDeskUserService;
import com.java.zhuiyun.api.member_manager.entity.MemberType;
import com.java.zhuiyun.api.member_manager.mapper.MemberTypeMapper;
import com.java.zhuiyun.api.order.entity.Orders;
import com.java.zhuiyun.api.order.mapper.OrderMapper;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.UploadFileUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
 * @ClassName FrontDeskUserServiceImpl
 * @Description TODO 前台用户管理实现类
 * @Author wuwan
 * @Date 2023/8/14 20:19
 **/
@Service
public class FrontDeskUserServiceImpl implements FrontDeskUserService {
    @Autowired
    private FrontDeskUserMapper frontDeskUserMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberTypeMapper memberTypeMapper;

    /*
    * @Author wuwan
    * @Description 前台用户数据查询
    * @Date 2023/8/14 20:33
    * @Return
    * @param null
    */
    @Override
    public PageResult getFrontDeskUser(String frontDeskName, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<FrontDeskUser> frontDeskUsers = frontDeskUserMapper.getFrontDeskUser(frontDeskName);
        PageResult pageResult = MybatisPageHelper.getPageResult(frontDeskUsers);
        return pageResult;
    }
    /*
    * @Author wuwan
    * @Description 账号封禁
    * @Date 2023/8/15 10:54
    * @Return
    * @param null
    */
    @Override
    public CommonResult updateFrontDeskUser(Integer id,String day) {
        //设置封禁时间
        LocalDate currentDate = LocalDate.now();
        LocalDate threeDaysLater = currentDate.plusDays(Integer.parseInt(day));
        //类型转换
        Date date = Date.from(threeDaysLater.atStartOfDay(ZoneId.systemDefault()).toInstant());
        Integer updateFrontDeskUser = frontDeskUserMapper.updateFrontDeskUser(id,date);
        if (updateFrontDeskUser>=1){
            return CommonResult.ok("禁用成功");
        }else {
            return CommonResult.error("禁用失败");
        }
    }
    /*
    * @Author wuwan
    * @Description 用户信息详细查看
    * @Date 2023/8/16 11:39
    * @Return
    * @param null
    */
    @Override
    public CommonResult queryById(Integer id) {
        return CommonResult.ok(frontDeskUserMapper.queryById(id));
    }

    /*
     * @Author WX
     * @Description 查询个人信息
     * @Date 2023/8/31 9:00
     * @Return
     * @param id
     */
    @Override
    public FrontDeskUser selectUserbyId(Integer id) {
        return frontDeskUserMapper.selectUserbyId(id);
    }

    /*
     * @Author WX
     * @Description 修改个人信息
     * @Date 2023/8/31 9:00
     * @Return
     * @param FrontDeskUser MultipartFile
     */
    @Override
    public int updateUserById(FrontDeskUser frontDeskUser, MultipartFile file) {
        if(file !=null){
        frontDeskUser.setImg(Integer.parseInt(getImage(file)));}
        frontDeskUser.setUpdateTime(new Date());
        return frontDeskUserMapper.updateUserById(frontDeskUser);
    }

    /*
     * @Author WX
     * @Description 查询会员购买与普通购买的差价
     * @Date 2023/8/31 9:01
     * @Return
     * @param null
     */
    @Override
    public FrontDeskUserModels getUserMemberManager(Integer id) {
        // 查询个人信息
        FrontDeskUserModels frontDeskUserModels =  frontDeskUserMapper.getUserMemberManager(id);
        // 查询个人购买订单
        List<Orders> list = orderMapper.selectAllOrders(id);
        BigDecimal totalOrderPrice = BigDecimal.ZERO;
        BigDecimal totalPaymentPrice = BigDecimal.ZERO;

       // 遍历集合并累加订单价格和支付价格
        for (Orders order : list) {
            totalOrderPrice = totalOrderPrice.add(order.getOrderPrice());
            totalPaymentPrice = totalPaymentPrice.add(order.getPaymentPrice());
        }

        BigDecimal priceDifference;
        if (list.isEmpty()) {
            priceDifference = BigDecimal.ZERO;
            frontDeskUserModels.setPriceDifference(priceDifference);
            return frontDeskUserModels;
        } else {
            priceDifference = totalOrderPrice.subtract(totalPaymentPrice);
            frontDeskUserModels.setPriceDifference(priceDifference);
            return frontDeskUserModels;
        }

    }

    /*
     * @Author WX
     * @Description 修改用户会员状态
     * @Date 2023/8/31 10:08
     * @Return
     * @param aliPay
     */
    @Override
    public int updateMember(AliPay aliPay) {
        // 获取对象中的会员类型id
        Integer id = aliPay.getMemberId();
        Integer userId = aliPay.getUserId();
        // 查询该类型id下的数据
        MemberType memberType = memberTypeMapper.fingById(id);
        FrontDeskUser frontDeskUser = frontDeskUserMapper.findTimeByUserId(userId);

        LocalDateTime currentDateTime = LocalDateTime.now(); // 获取当前时间

        if (frontDeskUser.getFrontDeskMemberTime() != null) {
            Date frontDeskMemberDate = frontDeskUser.getFrontDeskMemberTime();
            Instant instant = frontDeskMemberDate.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime frontDeskMemberDateTime = LocalDateTime.ofInstant(instant, zoneId);

            if (frontDeskMemberDateTime.isAfter(currentDateTime)) {
                // frontDeskMemberTime 大于当前时间
                LocalDateTime expireDateTime = frontDeskMemberDateTime.plus(memberType.getDuration(), ChronoUnit.MONTHS);
                Date expiryTime = Date.from(expireDateTime.atZone(ZoneId.systemDefault()).toInstant());
                aliPay.setExpiryTime(expiryTime);
            } else {
                // frontDeskMemberTime 小于等于当前时间
                LocalDateTime expireDateTime = currentDateTime.plus(memberType.getDuration(), ChronoUnit.MONTHS);
                Date expiryTime = Date.from(expireDateTime.atZone(ZoneId.systemDefault()).toInstant());
                aliPay.setExpiryTime(expiryTime);
            }
        }else {
            LocalDateTime expireDateTime = currentDateTime.plus(memberType.getDuration(), ChronoUnit.MONTHS);
            Date expiryTime = Date.from(expireDateTime.atZone(ZoneId.systemDefault()).toInstant());
            aliPay.setExpiryTime(expiryTime);
        }
        return frontDeskUserMapper.updateMember(aliPay);
    }

    /*
     * @Author WX
     * @Description 查询登录用户头像
     * @Date 2023/8/31 9:01
     * @Return
     * @param id
     */
    @Override
    public Integer getFrontUserImg(Integer id) {
        return frontDeskUserMapper.getFrontUserImg(id);
    }

    /*
     * @Author WX
     * @Description 修改邮箱
     * @Date 2023/9/1 9:44
     * @Return
     * @param null
     */
    @Override
    public CommonResult updateUserByEmail(FrontDeskUser frontDeskUser) {
        String frontDeskEmail = frontDeskUser.getFrontDeskEmail();
        FrontDeskUser frontDeskUser1 = frontDeskUserMapper.findByFrontDeskEmail(frontDeskEmail);
        if (frontDeskUser1 != null){
            return CommonResult.error();
        }else {
            return CommonResult.ok(frontDeskUserMapper.updateUserByEmail(frontDeskUser));
        }
    }

    /*
     * @Author WX
     * @Description 修改手机号
     * @Date 2023/9/1 9:44
     * @Return
     * @param null
     */
    @Override
    public CommonResult updateUserByPhone(FrontDeskUser frontDeskUser) {
        String frontDeskPhone = frontDeskUser.getFrontDeskPhone();
        FrontDeskUser frontDeskUser1 = frontDeskUserMapper.findByFrontDeskPhone(frontDeskPhone);
        if (frontDeskUser1 != null){
            return CommonResult.error();
        }else {
            return CommonResult.ok(frontDeskUserMapper.updateUserByPhone(frontDeskUser));
        }
    }

    /*
    * @Author wuwan
    * @Description 账号定时解封
    * @Date 2023/8/15 10:54
    * @Return
    * @param null
    */
    @Override
    public CommonResult updateAccount() {
        Integer updateAccount = frontDeskUserMapper.updateAccount();
        return CommonResult.ok(updateAccount);
    }
    /*
    * @Author wuwan
    * @Description 账号手动解封
    * @Date 2023/8/15 11:39
    * @Return
    * @param null
    */
    @Override
    public CommonResult updateAccountRelease(Integer id) {
        Integer updateAccount = frontDeskUserMapper.updateAccountRelease(id);
        return CommonResult.ok(updateAccount);
    }

    /*
     * @Author WX
     * @Description TODO 图片上传至服务器
     * @Date 2023/8/16 8:43
     * @Return
     * @param
     */
    public static String getImage(MultipartFile file){
        String s= UploadFileUtils.uploadFile(file);
        if (s != ""){
            return s;
        }
        return null;
    }
}
